import { reactive, readonly } from 'vue'

// 全局状态
const state = reactive({
  // 当前加载的图像
  currentImage: {
    file: null,
    url: null,
    width: 0,
    height: 0,
    loaded: false
  },

  // 测量数据
  measurements: {
    angles: [],
    distances: [],
    wbl: {
      calculated: false,
      preoperative: 0,
      target: 62,
      offset: 0,
      kneeWidth: 0
    }
  },

  // 标记点
  landmarks: {
    hip: null,
    ankle: null,
    knee_medial: null,
    knee_lateral: null
  },

  // 应用设置
  settings: {
    theme: 'light',
    language: 'zh-CN',
    autoSave: true,
    defaultWBLTarget: 62
  },

  // 当前病例
  currentCase: {
    id: null,
    patientName: '',
    patientAge: null,
    diagnosis: '',
    createdAt: null,
    updatedAt: null
  },

  // UI状态
  ui: {
    sidebarCollapsed: false,
    currentTool: 'none',
    showGrid: true,
    showMeasurements: true,
    zoomLevel: 100
  }
})

// 动作方法
const actions = {
  // 设置当前图像
  setCurrentImage(imageData) {
    Object.assign(state.currentImage, imageData)
  },

  // 清除当前图像
  clearCurrentImage() {
    state.currentImage = {
      file: null,
      url: null,
      width: 0,
      height: 0,
      loaded: false
    }
  },

  // 添加角度测量
  addAngleMeasurement(measurement) {
    state.measurements.angles.push({
      ...measurement,
      id: Date.now(),
      timestamp: new Date().toISOString()
    })
  },

  // 删除角度测量
  removeAngleMeasurement(id) {
    const index = state.measurements.angles.findIndex(m => m.id === id)
    if (index > -1) {
      state.measurements.angles.splice(index, 1)
    }
  },

  // 清除所有测量
  clearAllMeasurements() {
    state.measurements.angles = []
    state.measurements.distances = []
  },

  // 设置WBL数据
  setWBLData(wblData) {
    Object.assign(state.measurements.wbl, wblData)
  },

  // 设置标记点
  setLandmark(type, point) {
    state.landmarks[type] = point
  },

  // 清除标记点
  clearLandmarks() {
    state.landmarks = {
      hip: null,
      ankle: null,
      knee_medial: null,
      knee_lateral: null
    }
  },

  // 更新设置
  updateSettings(newSettings) {
    Object.assign(state.settings, newSettings)
  },

  // 设置当前病例
  setCurrentCase(caseData) {
    Object.assign(state.currentCase, caseData)
  },

  // 清除当前病例
  clearCurrentCase() {
    state.currentCase = {
      id: null,
      patientName: '',
      patientAge: null,
      diagnosis: '',
      createdAt: null,
      updatedAt: null
    }
  },

  // 更新UI状态
  updateUI(uiState) {
    Object.assign(state.ui, uiState)
  },

  // 切换侧边栏
  toggleSidebar() {
    state.ui.sidebarCollapsed = !state.ui.sidebarCollapsed
  },

  // 设置当前工具
  setCurrentTool(tool) {
    state.ui.currentTool = tool
  },

  // 设置缩放级别
  setZoomLevel(level) {
    state.ui.zoomLevel = Math.max(10, Math.min(500, level))
  }
}

// Getters
const getters = {
  // 获取当前图像是否已加载
  isImageLoaded: () => state.currentImage.loaded,

  // 获取所有角度测量
  allAngleMeasurements: () => state.measurements.angles,

  // 获取WBL是否已计算
  isWBLCalculated: () => state.measurements.wbl.calculated,

  // 获取所有标记点
  allLandmarks: () => state.landmarks,

  // 检查是否有足够的标记点进行WBL计算
  canCalculateWBL: () => {
    const { hip, ankle, knee_medial, knee_lateral } = state.landmarks
    return hip && ankle && knee_medial && knee_lateral
  },

  // 获取当前病例信息
  currentCaseInfo: () => state.currentCase,

  // 检查是否有当前病例
  hasCurrentCase: () => !!state.currentCase.id
}

// 创建存储实例
export const useStore = () => {
  return {
    state: readonly(state),
    actions,
    getters
  }
}

// 导出默认存储
export default useStore
